package com.qiyee.dalligent.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qiyee.dalligent.common.execption.BaseException;
import com.qiyee.dalligent.module.base.FileBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@Slf4j
public class UploadUtil {
    static   String LINUX_FILE_PATH="///data//new-dalligent//resume//";
    static String WINDOWS_FILE_PATH="d://dalligent//";
    static long MAX_FILE_SIZE=1024*1024*20;
    public static void main(String[] args) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHssSSS");
        //目标文件名称
        String targetName =sdf.format(new Date()) ;
        System.out.println(targetName);
    }

    private static String uploadFile(String targetPath,MultipartFile uploadFile) throws IOException {

        File file = new File(targetPath );
        if (!file.exists()){
            file.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHssSSS");
        //目标文件名称
        String targetName =sdf.format(new Date()) +getFileSuffix(uploadFile);
        log.info("文件名："+targetName);
        targetPath=targetPath+targetName;
        File targetFile = new File(targetPath );
        OutputStream fos = null;
        InputStream stream =null;
        try {
             fos =new BufferedOutputStream(new FileOutputStream(targetFile)) ;
            //获取本地文件输入流
            stream =new BufferedInputStream(uploadFile.getInputStream()) ;
            //写入目标文件
            byte[] buffer = new byte[1024*1024];
            int byteRead = 0;
            while((byteRead=stream.read(buffer))!=-1){
                fos.write(buffer, 0, byteRead);
                fos.flush();
            }

        } catch (IOException e) {
            fos.close();
            stream.close();
            e.printStackTrace();
        }
        finally {
            fos.close();
            stream.close();
        }
        return targetName;
    }

    private static String getFileSuffix(MultipartFile file) {
        if (file == null) {
            return null;
        }
        String fileName = file.getOriginalFilename();
        if (StringUtils.isBlank(fileName)) {
            return null;
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }
    public static FileBean uploadWindows( MultipartFile file,FileBean fileBean) throws IOException{
        fileBean.setFileName(uploadFile(WINDOWS_FILE_PATH,file));
        fileBean.setFilePath(WINDOWS_FILE_PATH);
        return fileBean;
    }
    public static FileBean uploadLinux( MultipartFile file,FileBean fileBean)throws IOException{
        fileBean.setFileName(uploadFile(LINUX_FILE_PATH,file));
        fileBean.setFilePath(LINUX_FILE_PATH);
        return fileBean;
    }
    public static FileBean uploadResumeFile(MultipartFile file){
        FileBean fileBean=new FileBean();
        long size = file.getSize();
        if (size>MAX_FILE_SIZE){
            throw new BaseException("文件太大，请压缩后上传");
        }
        fileBean.setSize(size +"");
        fileBean.setSuffix(getFileSuffix(file));
        try {
            if (OSinfo.isWindows()){
                return uploadWindows(file,fileBean);
            }
            if (OSinfo.isLinux()){
                return uploadLinux(file,fileBean);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileBean;
    }

}
